<%- include('../layout/header.ejs') %>
<link rel="stylesheet" href="../../public/css/mditor.min.css">
<script src="../../public/js/mditor.min.js"></script>
<div class="container">
    <h1>文章添加页</h1>
    <hr>
    <form id="form">
        <!-- 获取作者id 的隐藏域 -->
        <input type="hidden" name="authorId" value="<%=user.id%>">
        <div class="form-group">
            <label for="">文章标题:</label>
            <input type="text" name="title" class="form-control" required>
        </div>

        <div class="form-group">
            <label for="">文章内容:</label>
            <textarea name="content" class="form-control" id="editor"></textarea>
        </div>

        <div class="form-group">
                <input type="submit" value="发表文章" class="btn btn-primary">
            </div>
    </form>

</div>


<%- include('../layout/footer.ejs') %>
<script src="../../public/jquery.min.js"></script>
<script>
    $(function () {
        var mditor = Mditor.fromTextarea(document.getElementById('editor'));
        $('#form').on('submit',function(e){
            e.preventDefault()
            $.ajax({
                url:'/article/add',
                type:'post',
                data:$('#form').serialize(),
                datatype:'json',
                success:function(result){
                    if(result.status!==200) return alert('文章发表失败')
                    location.href = '/article/info/'+result.insertId
                }
            })
        })
    })
</script>